using System;
using System.Web.UI;
using umbraco;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.member;
using umbraco.presentation.nodeFactory;

namespace umbracoMemberControls
{
    /// <summary>
    ///		Summary description for dguGlemtKodeord.
    /// </summary>
    public partial class PasswordForgot : UserControl
    {
        private string _errorMessageNotFound = "No useraccount with that e-mail has been found!";
        private string _succes = "An email with the password has been sent to '%%EMAIL%%'";
        private string _emailFrom = "robot@umbraco.org";
        private string _emailSubject = "Password Reminder";

        private string _emailBody =
            "Hi %%NAME%%\n\nYour login is: '%%USERNAME%%' and password is '%%PASSWORD%%'\n\nKind regards\nThe Reminder Robot\n";

        public string ErrorMessageNotFound
        {
            set { _errorMessageNotFound = value; }
        }

        public string ConfirmMessage
        {
            set { _succes = value; }
        }

        public string EmailFrom
        {
            set { _emailFrom = value; }
        }

        public string EmailSubject
        {
            set { _emailSubject = value; }
        }

        public string EmailBody
        {
            set { _emailBody = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            mailError.Text = _errorMessageNotFound;
            email.Text = _succes;
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        #endregion

        private void sendPassword(string Email, string Name, string Login, string Password)
        {
            string body =
                _emailBody.Replace("%%NAME%%", Name).Replace("%%USERNAME%%", Login).Replace("%%PASSWORD%%", Password);
            library.SendMail(_emailFrom, Email, _emailSubject, body, false);
        }

        protected void ok_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    Member m = Member.GetMemberFromEmail(EmailAddress.Text);
                    if (m != null)
                    {
                        mailError.Text = "";
                        error.Visible = false;
                        sendPassword(m.Email, m.Text, m.LoginName, m.Password);
                        formular.Visible = false;
                        thankyou.Visible = true;
                    }
                    else
                        error.Visible = true;
                }
                catch (Exception ee)
                {
                    Log.Add(LogTypes.Error, Node.GetCurrent().Id,
                            string.Format("Error sending email reminder: {0}", ee.ToString()));
                    error.Visible = true;
                }
            }
        }
    }
}